package org.example.bighomework.SC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/SC")
public class SCController {
    @Autowired
    private SCService scService;
    //主页
    @GetMapping("/")
    public String viewHomePage(Model model){
        return findPaginated(1, "Sno", "asc", model);
    }
    //增加
    @GetMapping("/showSCForm")
    public String showSCForm(Model model) {
        SC sc = new SC();
        model.addAttribute("sc", sc);
        return "new_SC";
    }
    //保存和更新
    @PostMapping("/saveSC")
    public String saveSC(@ModelAttribute("sc")@Validated SC sc, BindingResult bindingResult, Model model) {
        //验证成绩是否合法
        if (bindingResult.hasErrors()) {
            return "update_SC";               //有不合法输入转到update_SC.html
        } else {
            scService.saveSC(sc);
            return "redirect:/SC/";                 //验证成功转至redirect:/SC/
        }

    }
    @GetMapping("/showFormForUpdate/{Sno}/{Cno}")
    public String showFormForUpdate(@PathVariable(value = "Sno") String Sno,@PathVariable(value = "Cno") String Cno, Model model) {
        SnoCnoPK snoCnoPK=new SnoCnoPK(Sno,Cno);
        SC sc = scService.getSCById(snoCnoPK);
        model.addAttribute("sc", sc);
        return "update_SC";
    }
    //删除
    @GetMapping("/deleteSC/{Sno}/{Cno}")
    public String deleteSC(@PathVariable(value = "Sno") String Sno,@PathVariable(value = "Cno") String Cno) {
        SnoCnoPK snoCnoPK=new SnoCnoPK(Sno,Cno);
        this.scService.deleteSCById(snoCnoPK);
        return "redirect:/SC/";
    }
    //按Sno查询
    @GetMapping("/query{Sno}")
    public String query(@PathVariable(value = "Sno") String Sno, Model model) {
        List<SC> listSC = scService.findBySno(Sno);
        model.addAttribute("listSC", listSC);
        return "SCIndex";
    }
    //获取分页数据
    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable(value = "pageNo") int pageNo,
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model) {
        int pageSize = 3;
        Page<SC> page = scService.findPaginated(pageNo, pageSize, sortField, sortDir);
        List<SC> listSC = page.getContent();
        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());
        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
        model.addAttribute("listSC", listSC);
        return "SCIndex";
    }
}
